﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Quartz;
using Quartz.Impl;
using log4net.Config;
using log4net;
using System.Collections.Specialized;
using Quartz.Impl.Calendar;
using Mina.Core.Service;
using Mina.Transport.Socket;
using Mina.Filter.Ssl;
using Mina.Filter.Logging;
using Mina.Filter.Codec;
using Mina.Filter.Codec.TextLine;
using System.Net;
using System.Configuration;

namespace TimerJobServer
{
    partial class MonitorServer : ServiceBase
    {
        private readonly ILog logger = LogManager.GetLogger(typeof(MonitorServer));
        private MinaServer mina = new MinaServer();
        private QuartzJobServer quartz = new QuartzJobServer();
        public MonitorServer()
        {
            InitializeComponent();
            LogHelp.ConfigLog4Net();//加载配置
            LogHelp.Watch();//监控日志信息

        }

        //private IScheduler sched;
        //通讯
        protected override void OnStart(string[] args)
        {
            try
            {
                var port = ConfigurationManager.AppSettings["SockPort"];
                int intPort;
                if(int.TryParse(port, out intPort))
                {
                    MinaServer.Port = intPort;
                }
                //启动服务监听
                mina.InitSockListening();
                //启动定时任务
                quartz.Start();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
            }

        }

        private void StartWithCode()
        {
            try
            {
                //1.首先创建一个作业调度池
                ISchedulerFactory sf = new StdSchedulerFactory();

                var sched = sf.GetScheduler();
                //2.创建出来具体的作业
                IJobDetail jobSafeyMonitor = null;//= 
                    //JobBuilder
                    //.Create<SafeyMonitorJob>()
                    //.WithIdentity("SafeyMonitor", "group1")
                    //.Build();
                //IJobDetail jobPersonnelOrientation = JobBuilder
                //    .Create<PersonnelOrientationJob>()
                //    .WithIdentity("PersonnelOrientation", "group1")
                //    .Build();
                //3.创建并配置触发器
                int intervalMinutes = int.Parse(System.Configuration.ConfigurationManager.AppSettings["IntervalMinutes"]);
                ISimpleTrigger trigger1 = (ISimpleTrigger)TriggerBuilder.Create()
                        .WithIdentity("trigger1", "group1")
                        .WithSimpleSchedule(x => x.WithIntervalInMinutes(intervalMinutes)
                                                     .RepeatForever()
                    //.WithRepeatCount(10)
                        )
                        .Build();
                DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, 30);
                ISimpleTrigger trigger2 = (ISimpleTrigger)TriggerBuilder.Create()
                        .StartAt(startTime)
                        .WithIdentity("trigger2", "group1")
                        .WithSimpleSchedule(x => x.WithIntervalInMinutes(intervalMinutes)
                                                    .RepeatForever()
                                                    )
                        .Build();
                //4.加入作业调度池中
                sched.ScheduleJob(jobSafeyMonitor, trigger1);
                //sched.ScheduleJob(jobPersonnelOrientation, trigger2);
                //5.开始运行
                sched.Start();
                logger.Info("服务启动:");
                logger.Info(jobSafeyMonitor.Key +
                     " and repeat: " + trigger1.RepeatCount +
                     " times, every " + trigger1.RepeatInterval.Minutes + " minutes");
                ////logger.Info(jobPersonnelOrientation.Key +
                //     " and repeat: " + trigger2.RepeatCount +
                //     " times, every " + trigger2.RepeatInterval.Minutes + " minutes");
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
            }
        }

        protected override void OnStop()
        {
            //停止服务
            quartz.Stop();
            mina.StopListening();
            logger.Info("服务停止");
        }
    }
}
